package com.arm.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arm.R;

public class AppTitle extends LinearLayout {
    private OnLeftButtonClickListener mLeftButtonClickListener;
    private OnRightButtonClickListener mRightButtonClickListener;
    private MyViewHolder mViewHolder;
    private View viewAppTitle;

    public AppTitle(Context context) {
        super(context);
        init();
    }

    public AppTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public AppTitle(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//获取布局
        viewAppTitle = inflater.inflate(R.layout.main_title, null);
        this.addView(viewAppTitle, layoutParams);

        mViewHolder = new MyViewHolder(this);
//点击事件
        mViewHolder.title_inter.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (mLeftButtonClickListener != null)
                {
                    mLeftButtonClickListener.onLeftButtonClick(v);
                }
            }
        });
//点击事件
        mViewHolder.title_right.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                if (mRightButtonClickListener != null)
                {
                    mRightButtonClickListener.OnRightButtonClick(v);
                }
            }
        });
    }
    //设置标题栏的控件显示与否
    public void initViewsVisible(boolean isLeftButtonVisile, boolean isCenterTitleVisile, boolean isRightIconVisile, boolean isRightTitleVisile) {
        // 左侧返回
        mViewHolder.ll_home.setVisibility(isLeftButtonVisile ? View.VISIBLE : View.INVISIBLE);

        // 中间标题
        mViewHolder.title_center.setVisibility(isCenterTitleVisile ? View.VISIBLE : View.INVISIBLE);
        //右侧两个
        mViewHolder.title_inter.setVisibility(isRightIconVisile ? View.VISIBLE : View.INVISIBLE);
        mViewHolder.title_right.setVisibility(isRightTitleVisile ? View.VISIBLE : View.INVISIBLE);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setAppTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mViewHolder.title_center.setText(title);
        }
    }

    public void setRightTitle(String text) {
        if (!TextUtils.isEmpty(text)) {
            mViewHolder.title_center.setText(text);
        }
    }

    public void setRightIcon(int sourceID) {

    }

    public void setLeftOnclick(int sourceID) {
        mViewHolder.title_inter.setText(sourceID);
    }

    public void setAppBackground(int color) {
        viewAppTitle.setBackgroundColor(color);
    }

    public void setOnLeftButtonClickListener(OnLeftButtonClickListener listen) {
        mLeftButtonClickListener = listen;
    }

    public void setOnRightButtonClickListener(OnRightButtonClickListener listen) {
        mRightButtonClickListener = listen;
    }

    public static abstract interface OnLeftButtonClickListener {
        public abstract void onLeftButtonClick(View v);
    }

    public static abstract interface OnRightButtonClickListener {
        public abstract void OnRightButtonClick(View v);
    }
    //获取id
    static class MyViewHolder
    {
        private final LinearLayout ll_home;
        private final TextView title_center;
        private final TextView title_inter;
        private final TextView title_right;

        public MyViewHolder(View v)
        {
            ll_home = v.findViewById(R.id.ll_home);
            title_center = v.findViewById(R.id.title_center);
            title_inter = v.findViewById(R.id.title_inter);
            title_right = v.findViewById(R.id.title_right);
        }
    }

}
